package com.javaspeak.designpatterns.go4.behavioural.strategy;

/**
 * The Strategy Pattern allows a strategy for doing something to be swapped
 * with another Strategy at runtime.
 * <p>
 * In this example we have a StrategyContext (ApplicationStrategy) where we
 * set the Strategy. We then call the executeStrategy() which in turn calls
 * the drawShape()  method on the underlying Strategy.
 * <p>
 * In this way, when we have a TriangleStrategy set we end up drawing a Triangle
 * as a shape and when we have a SquareStrategy set we draw a Triangle.
 *
 * @author John Dickerson
 */
public class ApplicationStrategy implements StrategyContext {

    private Strategy strategy;


    /* (non-Javadoc)
     * @see com.javaspeak.designpatterns.go4.behavioural.strategy.
     *      StrategyContext#setStrategy(com.javaspeak.designpatterns.
     *          go4.behavioural.strategy.Strategy)
     */
    @Override
    public void setStrategy( Strategy strategy ) {

        this.strategy = strategy;
    }


    /* (non-Javadoc)
     * @see com.javaspeak.designpatterns.go4.behavioural.strategy.
     *      StrategyContext#executeStrategy()
     */
    @Override
    public void executeStrategy() {

        strategy.drawShape();
    }


    /**
     * Set a SquareStrategy and call the drawShape() method which in turn draws
     * a Square.  Then set a TriangleStrategy which calls the drawShape()
     * method which in turn draws a Triangle.
     */
    public void runExample(){

        setStrategy( new SquareStrategy() );
        executeStrategy();
        setStrategy( new TriangleStrategy() );
        executeStrategy();
    }


    public static void main( String args[] ){

        ApplicationStrategy application = new ApplicationStrategy();
        application.runExample();
    }
}
